<?php
//todo: mail thing.
class Sponsors extends CI_Controller {
    //construct
	var $admin_email;
	
    public function __construct() {
		
		parent::__construct();
		
		$this->load->database();
		
		$this->admin_email = 'jose.palala@gmail.com';
		
    }
	public function index() {
		echo '<a href="'.site_url('sponsors/login').'">login here</a>';
	}
    //login page
	public function login() {
		$_SESSION['lang'] = 'en';

		//redirect elsewhere if username is already set
		if ( isset ($_SESSION['username']) ) 
		{
			if($_SESSION['usertype'] == 'scholar') {
				redirect('scholars/home');
			} else {
			//sponsorsponsor
				redirect('sponsors/home');
			}
		
		}
		
		if (isset ($_POST['email'])) {
			redirect('http://localhost/devcup/webgeek08/index.php/profiles/index/?student_id=1');
			}
		
		$this->form_validation->set_rules('password','Password', 'required|min_length[3]');

		if ( $this->form_validation->run() !== false) {
			
			
			
			$this->load->model('sponsor_db');
            
			$res = $this->sponsor_db->is_sponsor(
                                $this->input->post('username'), 
                                $this->input->post('password'));
							
                    if ($res !== false) {
                    
                        $_SESSION['username'] = $this->input->post('email_address');
                        
                        redirect('home/top'); // goes to top page
                    }
					else {
				$this->load->view('sponsors/login');
				}
		}
		$this->load->view ('sponsor');
		// $this->load->view('sponsors/login');	//sponsors login form
	}
	
	//welcome page once sponsor is logged in
	public function welcome() {
		$this->load->view('sponsors/welcome');
	}
	
	//show scholars in need
	public function _loadscholars() {
		$result = $this->db->query("SELECT * FROM scholars WHERE sponsored == 0");
		
		foreach($result->results() as $row){
			
		}
	}
	//ito sponsors regform
    
    function register() {
        $this->load->view('sponsors/register');
        
    }
    //from register page
    function add() {
		$username = $this->input->post('username');
		$orgname = $this->input->post('organization');
		$orgname = $this->input->post('org_email');
		$cfirstname = $this->input->post('contact_firstname');
		$clastname = $this->input->post('contact_lastname');
		$pwd = $this->input->post('password');
		$cemail = $this->input->post('contact_email');
		$cmobile = $this->input->post('contact_mobile');
		$clandline = $this->input->post('contact_landline');
		
		$q = "INSERT INTO sponsors VALUES (NULL,'".$orgname."','".$username."','".$pwd."','".$org_email."','".date('Y-m-d H:i:s')."',',NULL)";
		$this->db->query($q);
		
		$id= mysql_insert_id();
		
		$q = "INSERT INTO sponsors_contacts VALUES (NULL,".$id.",'".$cfirstname."','".$clastname."','".$cemail."','".$cmobile."','".$clandline."','".date('Y-m-d H:i:s')."',NOW())";
		$this->db->query($q);
		
		//simple thank you message
		$this->load->view('sponsors/add');
        
    }
    //page view for viewing about the sponsor
    function about($sponsorId)
    {
        $this->db->select('sponsors.sponsor_id, sponsors.organization, sponsor_contacts.firstname, sponsor_contacts.lastname, sponsorcontacts.mobile, sponsor_contacts.landline,sponsor_contacts.email');
        $this->db->from('sponsors');
        $this->db->join('sponsor_contacts','sponsor_contacts.sponsor_id = sponsors.sponsor_id');
           
        $this->db->where('sponsors.sponsor_id',$sponsorId );
           
        $q = $this->db->get();
		
		foreach($q->results() as $row)
		{
			$data['sponsor_id'] = $row->sponsor_id;
			$data['organization'] = $row->organization;
			$data['username'] = $row->username;
			$data['contact_firstname'] = $row->firstname;
			$data['contact_lastname'] = $row->lastname;
			$data['contact_mobile'] = $row->mobilename;
			$data['contact_landline'] = $row->landline;
			$data['contact_email'] = $row->email;
		}
		
        $this->load->view('sponsors/about',$data);
    }
    
	//send email to admin
    function message_admin($sponsorId) {
		$message = $this->input->post('message');
		
		$this->db->get_where('sponsor_contacts',array('sponsor_id' => $sponsorId),1,0);
		$r = $this->db->get();
		foreach($r->result() as $row)
		{
			$sponsor_email = $row->email;
		}
		
		mail($this->admin_email,'SPONSOR MESSAGAE',$message,'From: ' .$sponsor_email );
	}
    
}
?>